package com.heba.fragment.mine;

import android.os.Bundle;
import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.heba.adapter.FmPagerAdapter;
import com.heba.config.CommonConfig;
import com.heba.fragment.LazyFragment;
import com.heba.greenmulu.R;
import com.heba.utils.IndicatorLineUtil;

import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by jason on 2020/9/17.
 */
public class MineSelectFriendsFragment extends LazyFragment {

    /**
     * tablayout
     */
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;

    /**
     * fragment
     */
    private MineFansFragment mineFansFragment;
    private MineFollowerFragment mineFollowerFragment;
    private ArrayList<Fragment> fragments = new ArrayList<>();

    private int followingCount;
    private int followerCount;
    private String buddyUserID = "";

    /**
     * 实例化fragment
     * 接收参数
     *
     * @param UID
     * @return
     */
    public static MineSelectFriendsFragment newInstance(String UID) {
        MineSelectFriendsFragment fragment = new MineSelectFriendsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CommonConfig.USER_ID, UID);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 取出参数
     */
    private void parseArgument() {
        Bundle bundle = getArguments();
        buddyUserID = bundle.getString(CommonConfig.USER_ID, "");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_mine_select, container, false);
        parseArgument();
        initView(view);
        initFragments();
        setCustomTabView();
        return view;
    }

    @Override
    protected void lazyLoad() {

    }

    private void initView(View view) {
        tabLayout = view.findViewById(R.id.tabLayout);
        viewPager = view.findViewById(R.id.viewPager);
    }

    /**
     * initFragments
     */
    private void initFragments() {
        mineFollowerFragment = MineFollowerFragment.newInstance(buddyUserID);
        mineFansFragment = MineFansFragment.newInstance(buddyUserID);
        fragments.add(mineFollowerFragment);
        fragments.add(mineFansFragment);
    }

    /**
     * 设置自定义tab
     */
    private void setCustomTabView() {
        /**
         * 添加tab
         */
        for (int i = 0; i < 2; i++) {
            tabLayout.addTab(tabLayout.newTab());
        }

        /**
         * 关联viewpager,设置adapter
         */
        tabLayout.setupWithViewPager(viewPager, false);
        pagerAdapter = new FmPagerAdapter(fragments, getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        /**
         * 设置tab文字,必须在关联viewpager之后设置，否则文字不显示
         */
        Objects.requireNonNull(tabLayout.getTabAt(0)).setCustomView(getTabTextView(getResources().getString(R.string.mine_tab_follows)));
        Objects.requireNonNull(tabLayout.getTabAt(1)).setCustomView(getTabTextView(getResources().getString(R.string.mine_tab_fans)));

        /**
         * 设置指示线长度间隔(单位dp)
         */
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                IndicatorLineUtil.setIndicator(tabLayout, 0, 0);
            }
        });
    }

    /**
     * 获取itemview
     *
     * @param text
     * @return
     */
    private View getTabTextView(String text) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_tab_select_layout, null);
        TextView tv_text = view.findViewById(R.id.tv_text);
        tv_text.setText(text);
        return view;
    }

    public int getFollowingCount() {
        return followingCount;
    }

    public void setFollowingCount(int followingCount) {
        this.followingCount = followingCount;
        TextView tv_text = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tv_text);
        tv_text.setText(String.format(getResources().getString(R.string.mine_tab_follows1)+" ", followingCount));
    }

    public int getFollowerCount() {
        return followerCount;
    }

    public void setFollowerCount(int followerCount) {
        this.followerCount = followerCount;
        TextView tv_text = tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tv_text);
        tv_text.setText(String.format(getResources().getString(R.string.mine_tab_fans1)+" ", followerCount));
    }
}
